package cn.itsource.basic.task.service.impl;

import cn.itsource.basic.task.service.TaskService;
import cn.itsource.order.domain.OrderAdopt;
import cn.itsource.order.mapper.OrderAdoptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
* 定时任务执行类
* @author wangcp
* @date 2021/05/10 10:42
**/
@Service
public class TaskServiceImpl implements TaskService {

    @Autowired
    private OrderAdoptMapper orderAdoptMapper;


    @Async("taskExecutor")
    @Override
    public void executeRemindMessage() {

        /**
         * 超时关单的逻辑
         *
         * from 表 where 确认订单时间 < 当前时间的退后15分钟
         *
         */
        //1.查询未支付,并且时间超过15分钟的订单
        String minuteAgoTime = getMinuteAgoTime();

        List<OrderAdopt> orderTimeBefore = orderAdoptMapper.findOrderTimeBefore(minuteAgoTime);

        //2.修改状态的已取消
        for (OrderAdopt orderAdopt : orderTimeBefore) {
            orderAdopt.setState(2); //已取消

            orderAdoptMapper.updateByPrimaryKeySelective(orderAdopt);
        }

    }


    public static void main(String[] args) {
      //  System.out.println(getMinuteAgoTime());
    }

    public  String  getMinuteAgoTime () {
        String minute =  "" ;
        Date date = new Date();//获取当前时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, -30);// 30分钟前
        //获取到完整的时间
        minute =  new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
        return  minute;
    }

}
